package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.ArrayMap;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BabySyllabusBean;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.BabySyllabusPresenter;
import com.android.infantschool.model.mvp.presenter.BabySyllabusPresenterImpl;
import com.android.infantschool.model.mvp.view.BabySyllabusView;
import com.android.infantschool.ui.adapter.BabySyllabusAdapter;
import com.android.infantschool.util.ToastUtil;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\20 0020 15:32
 * @class describe
 */
public class BabySyllabusActivity extends BaseActivity implements BabySyllabusAdapter.OnLister, BabySyllabusView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_last_week)
    public TextView tvLastWeek;
    @BindView(R.id.view_last_week)
    public View viewLastWeek;
    @BindView(R.id.tv_this_week)
    public TextView tvThisWeek;
    @BindView(R.id.view_this_week)
    public View viewThisWeek;
    @BindView(R.id.tv_next_week)
    public TextView tvNextWeek;
    @BindView(R.id.view_next_week)
    public View viewNextWeek;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;
    @BindView(R.id.img_title)
    public ImageView imgTitle;

    private BabySyllabusAdapter adapter = null;
    private BabySyllabusPresenter syllabusPresenter = null;
    private int TYPE_LAYOUT = 2;//默认本周  1:上周  2:本周   3:下周

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, BabySyllabusActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_syllabus);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        syllabusPresenter = new BabySyllabusPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("周计划");
        imgTitle.setVisibility(View.VISIBLE);
        imgTitle.setImageResource(R.mipmap.icon_infrom_add);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BabySyllabusAdapter(this, this);
        recyclerView.setAdapter(adapter);
        Map<String, String> map = new ArrayMap<>();
        map.put("token", AppConfig.getToken());
        map.put("courseCode", "2");//本周
        syllabusPresenter.getBabySyllabus(map);
        showThisWeek();
    }

    @Override
    public void lister(int position, int type) {

    }

    @OnClick({R.id.img_back, R.id.tv_last_week, R.id.tv_this_week, R.id.tv_next_week, R.id.img_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.tv_last_week:
                showLastWeek();
                break;
            case R.id.tv_this_week:
                showThisWeek();
                break;
            case R.id.tv_next_week:
                showNextWeek();
                break;
            case R.id.img_title:
                startActivity(WeekCurriculumActivity.newIntent(this));
                break;
        }
    }

    @Override
    public void errorBabySyllabus(String error) {
        this.dismissLoading();
        adapter.clear();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successBabySyllabus(BaseBean baseBean) {
        this.dismissLoading();
        BabySyllabusBean syllabusBean = (BabySyllabusBean) baseBean;
        if (syllabusBean.getCode() == 0) {
            adapter.setData(syllabusBean.getData(), TYPE_LAYOUT);
        } else {
            adapter.clear();
            ToastUtil.showShortToast(this, syllabusBean.getMsg());
        }
    }

    private void showLastWeek() {
        tvLastWeek.setTextColor(0xFF007C70);
        tvThisWeek.setTextColor(0xFF000000);
        tvNextWeek.setTextColor(0xFF000000);
        viewLastWeek.setVisibility(View.VISIBLE);
        viewNextWeek.setVisibility(View.GONE);
        viewThisWeek.setVisibility(View.GONE);
        TYPE_LAYOUT = 1;
        this.showLoading();
        Map<String, String> map = new ArrayMap<>();
        map.put("token", AppConfig.getToken());
        map.put("courseCode", "1");//上周
        syllabusPresenter.getBabySyllabus(map);
    }

    private void showThisWeek() {
        tvThisWeek.setTextColor(0xFF007C70);
        tvLastWeek.setTextColor(0xFF000000);
        tvNextWeek.setTextColor(0xFF000000);
        viewLastWeek.setVisibility(View.GONE);
        viewNextWeek.setVisibility(View.GONE);
        viewThisWeek.setVisibility(View.VISIBLE);
        TYPE_LAYOUT = 2;
        this.showLoading();
        Map<String, String> map = new ArrayMap<>();
        map.put("token", AppConfig.getToken());
        map.put("courseCode", "2");//本周
        syllabusPresenter.getBabySyllabus(map);
    }

    private void showNextWeek() {
        tvNextWeek.setTextColor(0xFF007C70);
        tvThisWeek.setTextColor(0xFF000000);
        tvLastWeek.setTextColor(0xFF000000);
        viewLastWeek.setVisibility(View.GONE);
        viewNextWeek.setVisibility(View.VISIBLE);
        viewThisWeek.setVisibility(View.GONE);
        TYPE_LAYOUT = 3;
        this.showLoading();
        Map<String, String> map = new ArrayMap<>();
        map.put("token", AppConfig.getToken());
        map.put("courseCode", "3");//本周
        syllabusPresenter.getBabySyllabus(map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (syllabusPresenter != null) {
            syllabusPresenter.onDestroy();
            syllabusPresenter = null;
        }
    }
}